//
//  XJCommunityHistoryViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/1.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  社区--记录VC

#import "XJCommunityHistoryViewController.h"
#import "XJCommunityHistoryDetailViewController.h"


//View
#import "XJCommunityHistoryCellTableViewCell.h"

// Model
#import "XJCommunityHistoryModel.h"

//
#import "XJURLConst.h"


@interface XJCommunityHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJCommunityHistoryViewController
#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight - XJTopNavH) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        
        _tableView.rowHeight = 80;
        
        
        [_tableView registerNib:[UINib nibWithNibName:@"XJCommunityHistoryCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"XJCommunityHistoryCellTableViewCellId"];
        
//        [_tableView registerClass:[XJCommunityHistoryCellTableViewCell class] forCellReuseIdentifier:@"XJCommunityHistoryCellTableViewCellId"];
        
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navTitle = @"记录";
    
    [self.view addSubview:self.tableView];
    [self requestData];
}



#pragma mark - 三 网络请求
- (void)requestData {
    
    [XJHttpTool requestCommunityServeListWithCommunityId:self.communityId successH:^(id responseObjc) {

        [self.dataArray removeAllObjects]; // 先清空数据。
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSArray *service_listArr = responseObjc[@"data"][@"service_list"];
            for (NSDictionary *dic in service_listArr) {
                XJCommunityHistoryModel *model = [XJCommunityHistoryModel mj_objectWithKeyValues:dic];
                [model setValuesForKeysWithDictionary:dic];
                
                [self.dataArray addObject:model];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
       
    } failH:^(NSError *error) {
        XJLog(@"%@",error);
    }];
}

// 点击cell上的删除按钮
- (void)requestDeleteAction:(NSString *)serviceId {
    [XJHttpTool requestCommunityServiceCancelWithURLPath:XJCommunityServiceDelete Service_id:serviceId successH:^(id responseObjc) {
        XJLog(@"%@",responseObjc);
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            [MBProgressHUD showSuccess:responseObjc[@"data"]];
            
            // 刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                [self requestData];
                [self.tableView reloadData];
            });
            
        }else{
            [MBProgressHUD showError:responseObjc[@"message"]];
        }
    } failH:^(NSError *error) {
        [MBProgressHUD showError:[error localizedDescription]];
    }];
}



#pragma mark - 四 代理方法 UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XJCommunityHistoryCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XJCommunityHistoryCellTableViewCellId"];
    
    XJCommunityHistoryModel *model = self.dataArray[indexPath.row];
    
    NSString *name  = model.linkman;
    NSString *stateName = model.state_name;
    NSString *btnTitle = model.buttonTitle;
    NSString *time = model.create_time;
    [cell configureName:name statusName:stateName timeStr:time buttonTitle:btnTitle];
    
    XJWeakSelf(self)
    // 事件
    cell.clickButtonBlock = ^{
        [weakself requestDeleteAction:model.service_id];
    };
    
    return cell;
}

#pragma mark - 四 代理方法 UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    XJCommunityHistoryModel *model = self.dataArray[indexPath.row];
    
    XJCommunityHistoryDetailViewController *detailVC = [[XJCommunityHistoryDetailViewController alloc] init];
    detailVC.serveId = model.service_id;
    
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
